package com.yuqing.sentimentapp.home.presenter.impl;

import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.home.acitivity.SearchActivity;
import com.yuqing.sentimentapp.home.bean.HotChannelBean;
import com.yuqing.sentimentapp.home.presenter.YqSearchPresenter;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.yqlist.bean.YqListResultBean;
import com.yuqing.sentimentapp.yqlist.fragment.YqListFragment;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class YqSearchListPresenterImpl implements YqSearchPresenter {

    private SearchActivity searchActivity;

    public YqSearchListPresenterImpl(SearchActivity searchActivity) {
        this.searchActivity = searchActivity;
    }

    @Override
    public void loadYqSearchListData(int page, int pageSize, String keyword) {
        ModuleManager.getInstance().loadYqSearchListResult(searchActivity, page, pageSize, keyword)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<YqListResultBean>() {
                    @Override
                    protected void hideDialog() {
                        searchActivity.stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        searchActivity.startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(YqListResultBean yqListResultBean) {
                        if (checkContext()) return;
                        searchActivity.returnSearchListResult(yqListResultBean);
                    }

                    @Override
                    protected void onFailure(String message) {
                        searchActivity.onFailure(message);
                    }
                });
    }

    @Override
    public void loadHotChanneltData() {
        ModuleManager.getInstance().loadHotChannelResult(searchActivity)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<HotChannelBean>() {
                    @Override
                    protected void hideDialog() {
                        searchActivity.stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        searchActivity.startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(HotChannelBean hotChannelBean) {
                        if (checkContext()) return;
                        searchActivity.returnHotChannelResult(hotChannelBean);
                    }

                    @Override
                    protected void onFailure(String message) {
                        searchActivity.onFailure(message);
                    }
                });
    }

    private boolean checkContext() {
        if (searchActivity.isFinishing() || searchActivity.isDestroyed())
            return true;
        return false;
    }
}
